package com.ljh.extensions.rabbit.config;

import com.ljh.extensions.rabbit.constants.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: Themberfue
 * @date: 2025/5/2 9:44
 * @description:
 */
@Configuration
public class DlConfig {
    @Bean("normalQueue")
    public Queue normalQueue() {
        return QueueBuilder.durable(Constants.NORMAL_QUEUE)
                .deadLetterExchange(Constants.DL_EXCHANGE) // ? 配置该队列的死信交换机
                .deadLetterRoutingKey("dl") // ? 死信交换机绑定死信队列的 Routing Key
                .ttl(10_000)
                .maxLength(10L) // ? 设置队列最大长度
                .build();
    }
    @Bean("normalExchange")
    public DirectExchange normalExchange() {
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE)
                .durable(true)
                .build();
    }
    @Bean("normalBinding")
    public Binding normalBinding(@Qualifier("normalExchange") DirectExchange exchange, @Qualifier("normalQueue") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("normal");
    }
    @Bean("dlQueue")
    public Queue dlQueue() {
        return QueueBuilder.durable(Constants.DL_QUEUE)
                .build();
    }
    @Bean("dlExchange")
    public DirectExchange dlExchange() {
        return ExchangeBuilder.directExchange(Constants.DL_EXCHANGE)
                .durable(true)
                .build();
    }
    @Bean("dlBinding")
    public Binding dlBinding(@Qualifier("dlExchange") DirectExchange exchange, @Qualifier("dlQueue") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("dl");
    }
}
